package com.chinatgl.latte.ec.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.API;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.adapter.ShopGoodsRecycleViewAdapter;
import com.chinatgl.latte.ec.mode.Product;
import com.chinatgl.latte.ec.params.MapUtil;
import com.chinatgl.latte.net.NetRequest;
import com.chinatgl.latte.net.listener.onNetRquestListener;
import com.chinatgl.latte.util.json.Result;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;



/**
 * 文件描述:
 * 作者: Created by 杨伟乔 on 2017/8/11 0011.
 * 版本号: 1.0
 */


public class RecommendDelegate extends LatteDelegate {
    @Override
    public Object setLayout() {
        return R.layout.delegate_recommen;

    }

    int pageNo = 1;
    int pageSize = 6;
    private ArrayList<Product.ProductBean> recommendlist;
    private boolean isGetMore = true;
    private ShopGoodsRecycleViewAdapter adapter;
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        //推荐列表
        RecyclerView rvRecommen = (RecyclerView) rootView.findViewById(R.id.rv_recommen);
        recommendlist = new ArrayList<>();
        adapter = new ShopGoodsRecycleViewAdapter(recommendlist);
        rvRecommen.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        rvRecommen.setHasFixedSize(true);
        rvRecommen.setAdapter(adapter);
        rvRecommen.setNestedScrollingEnabled(false);


        WeakHashMap<String, Object> params = MapUtil.getParams();
        params.put("pageNo", pageNo);
        params.put("pageSize", pageSize);
        Type type = new TypeToken<Result<List<Product.ProductBean>>>() {
        }.getType();
        NetRequest.post(API.GUESSLIKE, params, type, true, 60 * 24, new onNetRquestListener() {
            @Override
            public void requestdataFinish(String catalog, Object bean) {
                if (bean != null) {
                    Result<List<Product.ProductBean>> result = (Result<List<Product.ProductBean>>) bean;

                    if (result.getCode() == 0) {

                        List<Product.ProductBean> result1 = result.getResult();

                        if (result1 != null && result1.size() > 0) {
                            if (pageNo == 1) {
                                recommendlist.clear();
                            }
                            recommendlist.addAll(result1);
                            adapter.notifyDataSetChanged();
                        } else {

                            isGetMore = false;

                        }


                    }

                }
            }

            @Override
            public void requestdataFailed(String msg, Throwable e) {

            }

            @Override
            public void requestComplete() {

            }
        },getActivity());

    }
}
